package main

import (
	"fmt"
	"log"
	"time"

	"xorm.io/xorm"

	_ "gitee.com/general252/kingbase-xorm1"
)

type Person struct {
	UID   string `xorm:"'uid' not null unique VARCHAR(64)"`
	Name  string
	Age   int
	Index int
}

func main() {

	const (
		host     = "192.168.88.88"
		port     = 54321
		user     = "system"
		password = "123456"
		dbname   = "test"
	)

	connInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname) // kingbase
	connInfo = fmt.Sprintf("kingbase://%v:%v@%v:%v/%v?sslmode=disable", user, password, host, port, dbname)

	engine, err := xorm.NewEngine("kingbase", connInfo)
	if err != nil {
		panic(err)
	}

	engine.ShowSQL(true)

	if err = engine.Ping(); err != nil {
		panic(err)
	}

	if err = engine.Sync2(new(Person)); err != nil {
		panic(err)
	}

	fmt.Println("Connection test success!")

	_, err = engine.Transaction(func(session *xorm.Session) (interface{}, error) {
		return session.Insert(&Person{
			UID:   time.Now().Format(time.RFC3339),
			Name:  "bb",
			Age:   11,
			Index: 22,
		})
	})
	if err != nil {
		panic(err)
	}

	var person Person
	ok, err := engine.Get(&person)
	log.Println(ok, err, person)

	_ = engine.Close()
}
